﻿using System.Net.Http.Headers;
using Swimj.RazorLib.Caches;

namespace Client.BlazorWasmApp.Authentication
{
    public class AuthHeaderHandler : DelegatingHandler
    {
        private readonly ISecureCache _secureCache;

        public AuthHeaderHandler(ISecureCache secureCache)
        {
            _secureCache = secureCache;
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var token = await _secureCache.GetAsync("authToken");

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

            return await base.SendAsync(request, cancellationToken);
        }

    }
}